﻿// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
string name = "TcpClient";
Console.WriteLine(name);
int sendport = 2001, reveiveport = 2002;
HPSocket.Tcp.TcpClient sockets = new();

TcpClient tcpClient = new TcpClient();

System.Net.Sockets.UdpClient sendUDP;
System.Net.Sockets.UdpClient receiveUDP;

sendUDP = new System.Net.Sockets.UdpClient();
receiveUDP = new System.Net.Sockets.UdpClient(reveiveport);


while (true)
{
    var key = Console.ReadLine();
    if (key == "1")
    {
        Console.WriteLine(sockets.IsConnected);
    }
    else if (key == "2")
    {
        try
        {
            var port=Console.ReadLine();
            var linkport= ushort.Parse(port.Trim());
            //sockets.Connect("10.10.100.254", 8899);
            sockets.Connect("192.168.1.55", linkport);
            sockets.OnReceive += Sockets_OnReceive;
        }
        catch (Exception e)
        {
            Console.WriteLine(
            e);
        }
    }
    else if (key == "4")
    {
        string order = "01 03 80 0E 00 04 0C 0A";
       // string order = "01 03 80 0E 00 04 0C 0A";
        var byteorder = ModbusDevicesHelp.StringToBytes(order);
        var dst = sockets.Send(byteorder, byteorder.Length);
        Console.WriteLine(dst);
    }
    else if (key == "5")
    {
        string order = "01 10 00 00 00 01 02 31 32 32 15";
        var byteorder = ModbusDevicesHelp.StringToBytes(order);
        var dst = sockets.Send(byteorder, byteorder.Length);
        Console.WriteLine(dst);
    }
    else if (key == "6")
    {
        string order = "01 03 00 00 00 08 44 0C";
        var byteorder = ModbusDevicesHelp.StringToBytes(order);
        var dst = sockets.Send(byteorder, byteorder.Length);
        Console.WriteLine(dst);
    }
    else if (key == "7")
    {
        var data = File.ReadAllBytes("C:\\Users\\Administrator\\Pictures\\20180913005.png");
        var dst = sockets.Send(data, data.Length);
        Console.WriteLine(dst);
    }

    if (key == "9")
    {

        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
        //sendUDP.Connect(endpoint);
        //endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
        //receiveUDP.Connect(endpoint);
        receiveUDP = new System.Net.Sockets.UdpClient(endpoint);
    }
    if (key == "10")
    {
        Task.Factory.StartNew(() =>
        {
            while (true)
            {
                using (UdpClient udp=new UdpClient())
                {
                    IPEndPoint sendPort = new IPEndPoint(IPAddress.Parse("127.0.0.1"), sendport);
                    var data = Encoding.UTF8.GetBytes($"发送端口:{sendport}-{name}{DateTime.Now.ToString()}");
                    //udpClient.BeginSend(data, data.Length, remoteEndPoint, null, null);
                    udp.Send(data, data.Length, sendPort);
                }
                using (UdpClient udp = new UdpClient())
                {
                    IPEndPoint sendPort = new IPEndPoint(IPAddress.Parse("127.0.0.1"), sendport+2);
                    var data = Encoding.UTF8.GetBytes($"发送端口:{sendport + 2}-{name}{DateTime.Now.ToString()}");
                    //udpClient.BeginSend(data, data.Length, remoteEndPoint, null, null);
                    udp.Send(data, data.Length, sendPort);
                }


                //if (receiveUDP.Available > 0)
                //{
                //    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), sendport);
                //    var dst = receiveUDP.Receive(ref remoteEndPoint);
                //    string receivedData = Encoding.UTF8.GetString(dst);
                //    Console.WriteLine($"{name}-接收端口:{remoteEndPoint.Port}+ {receivedData}");
                //}

                Thread.Sleep(1000);
            }
        });
    }
}

HPSocket.HandleResult Sockets_OnReceive(HPSocket.IClient sender, byte[] data)
{
    Console.WriteLine(ModbusDevicesHelp.BytesToString(data));
    return HPSocket.HandleResult.Ok;
}